
import { requestToExport } from '@/api/commonHttp';
export default {
  data () {
    return {
      qualificationFileType: '', // 文件类型
      btnLoading: {
        download: false,
      }
    }
  },
  methods: {
    // 文件类型切换
    handleChangeTypeByQuaFile () {
      if (this.isFileTypeByPdf()) {
        this.formData.imageList = [];
        this.defaultList = [];
      }
      if (this.isFileTypeByImage()) {
        this.formData.fileList = [];
        this.uploadFileList = [];
      }
    },
    // 文件类型是pdf
    isFileTypeByPdf () {
      return this.qualificationFileType === '1';
    },
    // 文件类型是图片
    isFileTypeByImage () {
      return this.qualificationFileType === '2';
    },
    // pdf为空
    isEmptyPdf () {
      return this.formData.fileList && this.formData.fileList.length === 0;
    },
    // 图片为空
    isEmptyImages () {
      return this.formData.imageList && this.formData.imageList.length === 0;
    },
    // 当前文件类型为空
    isEmptyFileByType () {
      if (this.isEmptyPdf() && this.isEmptyImages()) { return true; }
      if (this.isFileTypeByPdf()) { return this.isEmptyPdf(); }
      if (this.isFileTypeByImage()) { return this.isEmptyImages(); }
      return false;
    },
    // 获取资质文件类型
    getQualificationFileType () {
      if (!this.isEmptyPdf()) { return '1'; }
      if (!this.isEmptyImages()) { return '2'; }
      return '';
    },
    // 获取文件urls集合
    getUrlsByFiles () {
      if (this.isFileTypeByImage()) {
        return this.formData.imageList.map(item => item.previewUrl).filter(item => item);
      }
      if (this.isFileTypeByPdf()) {
        return this.formData.fileList.map(item => item.previewUrl).filter(item => item);
      }
      return [];
    },
    // 一键下载
    async handleDownloadFiles () {
      if (this.isEmptyFileByType()) {
        return this.$Message.warning('没有可下载的文件');
      }
      const res = await requestToExport({
        url: '/sysback/fileserver/downloadZip',
        type: 'post',
        data: { urls: this.getUrlsByFiles() }
      }, () => { this.btnLoading.download = true; }, () => { this.btnLoading.download = false; });
      const blob = new Blob([res], {
        type: 'application/octet-stream'
      });
      let link = document.createElement('a');
      link.href = window.URL.createObjectURL(blob);
      link.download = `下载附件.zip`;
      link.click();
      window.URL.revokeObjectURL(blob); // 释放掉blob对象
    },
  }
}
